package com.example.java_chatroom.controller;

import com.example.java_chatroom.mapper.UserMapper;
import com.example.java_chatroom.model.User;
import com.example.java_chatroom.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
//实现注册登录
@RequestMapping("/user")
public class UserController {
//    @Resource
//    UserMapper userMapper;
    @Resource
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public User login(String username, String password, HttpServletRequest request){
        //1、先去数据库中查找，看username是否能找到对应的user对象
        //如果可以找到则看一下密码是否匹配
        //2、如果都匹配则创建会话!!
        User user = userService.selectByName(username);
        if(user==null||!user.getPassword().equals(password)){
            //登录失败同时返回空对象
//            user.setUsername("");
//            user.setPassword("");
            System.out.println("当前登录失败，用户名或者密码错误"+user);

            return new User();
        }
        System.out.println("当前登录成功"+user);
        //下面一条语句ture表示会话不存在就创建
        HttpSession httpSession=request.getSession(true);
        httpSession.setAttribute("user",user);

        //返回之前password干掉，避免不必要的麻烦
        user.setPassword("");
        return user;
    }

    @PostMapping("/register")
    @ResponseBody
    public User register(String username,String password){
        //自己
//        User user = userService.selectByName(username);
//        if(user==null){
//            user.setUsername(username);
//            user.setPassword(password);
//            userService.insert(user);
//            System.out.println("user"+user);
//            return user;
//        }
//        System.out.println("注册失败");
//        return null;

        User user= null;
        try {
            user = new User();
            user.setUsername(username);
            user.setPassword(password);
            int ret=userService.insert(user);
            System.out.println("注册 ret:"+ret);
            user.setPassword("");
        } catch (DuplicateKeyException e) {
            //所以当前inset方法抛出上述异常，说明名字重复
            user=new User();
        }
        return user;
    }

    @GetMapping("/userInfo")
    @ResponseBody
    public User getUserInfo(HttpServletRequest req) {
        // 1. 先从请求中获取到会话
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 会话不存在, 用户尚未登录. 此时返回一个空的对象即可.
            System.out.println("[getUserInfo] 当前获取不到 session 对象!");
            return new User();
        }
        // 2. 从会话中获取到之前保存的用户对象.
        User user = (User) session.getAttribute("user");
        if (user == null) {
            System.out.println("[getUserInfo] 当前获取不到 user 对象!");
            return new User();
        }
        user.setPassword("");
        return user;
    }
}
